package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.CategoryAddNewDTO;
import cn.tedu.csmall.product.pojo.vo.AlbumListItemVO;
import cn.tedu.csmall.product.pojo.vo.CategoryListItemVO;
import cn.tedu.csmall.product.pojo.vo.CategoryStandardVO;
import cn.tedu.csmall.product.service.ICategoryService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Slf4j
@Validated
@Api(tags = "类别管理模块")
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private ICategoryService service;

    @ApiOperation("添加类别")
    @PostMapping("/addNew")
    public JsonResult<Void> addNew(@Valid CategoryAddNewDTO addNewDTO) {
        log.debug("需要添加类别的参数是：{}", addNewDTO);
        service.addNew(addNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("根据id删除类别")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> deleteById(@PathVariable Long id) {
        log.debug("需要删除的类别id是：{}", id);
        service.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("根据id启动类别")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "类别id", required = true, example = "9527", dataType = "long")
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult<Void> enableById(@Range(min = 1, max = 1000000, message = "启用类别失败，类别id非法！")
                                       @PathVariable Long id) {
        log.debug("需要启动的类别id是：{}", id);
        service.setEnable(id);
        return JsonResult.ok();
    }

    @ApiOperation("根据id禁用类别")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "类别id", required = true, example = "9527", dataType = "long")
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult<Void> disableById(@Range(min = 1, max = 1000000, message = "启用类别失败，类别id非法！")
                                        @PathVariable Long id) {
        log.debug("需要禁用的类别id是：{}", id);
        service.setDisable(id);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}/select")
    @ApiOperation("根据Id查询详情")
    public JsonResult<CategoryStandardVO> select(@PathVariable Long id){
        log.debug("开始处理根据ID查询类别详情，id是：{}",id);
        CategoryStandardVO standardVO = service.getStandard(id);
        return JsonResult.ok(standardVO);
    }

    @GetMapping("/list-by-parentId")
    @ApiOperation("根据parentId查询列表")
    public JsonResult<List<CategoryListItemVO>> list(Long parentId){
        log.debug("开始处理查询类别列表，无参数");
        List<CategoryListItemVO> list = service.listByParentId(parentId);
        return JsonResult.ok(list);
    }
}
